package com.infoDiffusion.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.infoDiffusion.bean.EmergencyBean;
import com.infoDiffusion.bean.KnowledgeBean;
import com.infoDiffusion.bean.MaterialBean;
import com.infoDiffusion.bean.UserBean;
import com.infoDiffusion.service.AdminService;
import com.infoDiffusion.service.ReportersService;
import com.infoDiffusion.util.Constants;
import com.infoDiffusion.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ReportersAction extends ActionSupport {
	@Autowired
	ReportersService rService;
	@Autowired
	AdminService adminService;
	KnowledgeBean kbean;
	MaterialBean mbean;
	EmergencyBean emergencyBean;
	UserBean userbean;
	public HttpServletResponse response = ServletActionContext.getResponse();
	public HttpServletRequest request = ServletActionContext.getRequest();
	public HttpSession session = request.getSession();

	// 添加应急知识
	public String addkknowledge() {
		userbean = (UserBean) request.getSession().getAttribute(Constants.SESSION_ADMIN);
		kbean.setUserBean(userbean);
		System.out.println(kbean);
		boolean flag = rService.addk(kbean);
		if (flag)
			return "addsucck";
		else
			return "addfalsk";
	}

	// 应急知识列表
	public String listk() {
		userbean = (UserBean) request.getSession().getAttribute(Constants.SESSION_ADMIN);
		ActionContext.getContext().put("bean", userbean);
		List<KnowledgeBean> list = rService.listk(kbean);
		ActionContext.getContext().put("list", list);
		return "listk";
	}

	// 获取ID及bean数据
	public String toupdatek() {
		int id = StringUtil.toInt(request.getParameter("id"));
		if (id > 0) {
			KnowledgeBean bean = rService.getById(id);
			ActionContext.getContext().put("bean", bean);
		}
		return "toUpdatek";

	}

	// 更新应急知识
	public String updateknowledge() {
		boolean flag = rService.updateknowledge(kbean);
		if (flag)
			return "updateok";
		else
			return "updatefalse";
	}

	// 删除应急知识
	public String deletek() {
		int id = StringUtil.toInt(request.getParameter("id"));
		if (id > 0) {
			KnowledgeBean bean = rService.getById(id);
			boolean flag = rService.delete(bean);
			if (flag)
				return "delete";
		}
		return "flase";
	}
	
	public String toAddm(){
		userbean = (UserBean) request.getSession().getAttribute(Constants.SESSION_ADMIN);
		ActionContext.getContext().put("bean", userbean);
		return "toaddm";
		
	}
	public String addmaterial() {
		userbean = (UserBean) request.getSession().getAttribute(Constants.SESSION_ADMIN);
		int id = userbean.getId();
		int power = userbean.getPower();
		if(power==0){
			mbean.setDept("系统管理员");
		}else if(power==1){
			mbean.setDept("应急办工作人员");
		}else if(power==2){
			mbean.setDept("地震局工作人员");
		}else if(power==3){
			mbean.setDept("气象局工作人员");
		}else if(power==4){
			mbean.setDept("水文局工作人员");
		}
		mbean.setUserBean(userbean);
		boolean flag = rService.addm(mbean);
		if (flag)
			return "addsucc1";
		else
			return "addfals1";
	}

	public String listm() {
		userbean = (UserBean) request.getSession().getAttribute(Constants.SESSION_ADMIN);
		ActionContext.getContext().put("bean", userbean);
		List<MaterialBean> listcount = rService.listmcount(mbean);
		ActionContext.getContext().put("listcount", listcount);
		List<MaterialBean> list = rService.listm(mbean);
		ActionContext.getContext().put("list", list);
		
		return "listm";
	}

	public String toUpdatem() {
		int userId = StringUtil.toInt(request.getParameter("userId"));
		UserBean ubean = adminService.getById(userId);
		ActionContext.getContext().put("bean", ubean);
		int id = StringUtil.toInt(request.getParameter("id"));
		if (id > 0) {
			MaterialBean list = rService.getByIdm(id);
			ActionContext.getContext().put("list", list);
		}
		return "toUpdatem";

	}

	// 更新资源
	public String updatem() {
		int userId  =StringUtil.toInt(request.getParameter("userId"));
		UserBean bean = adminService.getById(userId);
		int power = bean.getPower();
		if(power==0){
			mbean.setDept("系统管理员");
		}else if(power==1){
			mbean.setDept("应急办工作人员");
		}else if(power==2){
			mbean.setDept("地震局工作人员");
		}else if(power==3){
			mbean.setDept("气象局工作人员");
		}else if(power==4){
			mbean.setDept("水文局工作人员");
		}
		mbean.setUserBean(bean);
		boolean flag = rService.updatem(mbean);
		if (flag)
			return "updateok1";
		else
			return "updatefalse1";
	}

	// 删除资源信息
	public String deletem() {
		int id = StringUtil.toInt(request.getParameter("id"));
		if (id > 0) {
			MaterialBean bean = rService.getByIdm(id);
			boolean flag = rService.delete(bean);
			if (flag)
				return "delete1";
		}
		return "flase";
	}
	//添加突发事件
	public String addEmergency() {
		userbean = (UserBean) request.getSession().getAttribute(Constants.SESSION_ADMIN);
		emergencyBean.setUserBean(userbean);
		boolean flag = rService.adde(emergencyBean);
		if (flag)
			return "addsucc2";
		else
			return "addfals2";
	}

	public String listEmergency() {
		userbean = (UserBean) request.getSession().getAttribute(Constants.SESSION_ADMIN);
		//获取发布员信息
		ActionContext.getContext().put("bean", userbean);
		//获取突发事件列表信息
		List<EmergencyBean> list = rService.liste(emergencyBean);
		ActionContext.getContext().put("list", list);
		return "listEmergency";
	}

	public String toUpdateEmergency() {
		//根据userid获取bean 再获取相应的power
		int userId  =StringUtil.toInt(request.getParameter("userId"));
		UserBean bean = adminService.getById(userId);
		ActionContext.getContext().put("bean", bean);
		int id = StringUtil.toInt(request.getParameter("id"));
		if (id > 0) {
			EmergencyBean list = rService.getByIde(id);
			ActionContext.getContext().put("list", list);
		}
		return "toUpdateEmergency";

	}

	// 更新
	public String updateEmergency() {
//		int userId  =StringUtil.toInt(request.getParameter("userId"));
//		UserBean bean = adminService.getById(userId);
//		emergencyBean.setUserId(userId);
//		System.out.println(userId);
		
		boolean flag = rService.updatee(emergencyBean);
		if (flag)
			return "updateok2";
		else
			return "updatefalse2";
	}

	// 删除突发事件
	public String deleteEmergency() {
		int id = StringUtil.toInt(request.getParameter("id"));
		if (id > 0) {
			EmergencyBean bean = rService.getByIde(id);
			boolean flag = rService.delete(bean);
			if (flag)
				return "delete2";
		}
		return "flase";
	}
	
//	public KnowledgeBean getModel() {
//		return kbean;
//	}
	
	public KnowledgeBean getKbean() {
		return kbean;
	}

	public void setKbean(KnowledgeBean kbean) {
		this.kbean = kbean;
	}

	public EmergencyBean getEmergencyBean() {
		return emergencyBean;
	}

	public void setEmergencyBean(EmergencyBean emergencyBean) {
		this.emergencyBean = emergencyBean;
	}

	public MaterialBean getMbean() {
		return mbean;
	}

	public void setMbean(MaterialBean mbean) {
		this.mbean = mbean;
	}

	

}
